/* @(#)GSAfficheDAO.java    V0.1
 *
 * Project: NEWGS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-5-24
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.dao;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;

import com.hitrust.gs.bean.GSAfficheBean;
import com.hitrust.gs.form.GSAfficheForm;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.exception.GSException;

/**
 * 
 */
public class GSAfficheDAO extends GSBaseDAO{
    /**
     * 查询所有公告表
     * @return
     * @throws GSException
     */
    public ArrayList queryAllAffiche() throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from GS_AFFICHE order by GS_AFFICHE_TIME desc";
        try{
            tList = this.executeQuery(new GSAfficheBean(),tSql,null);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    /**
     * 在公告表GS_AFFICHE中新增一笔记录
     * 
     * @param aGSBalanceFeeBean
     * 
     * @return boolean
     * 
     * @throws CUPSException
     */
    public boolean add(GSAfficheBean aGSAfficheBean) throws GSException {

        String tSql="INSERT INTO GS_AFFICHE (GS_AFFICHE_ID,GS_AFFICHE_TITLE,GS_AFFICHE_CONTENT," +
                "GS_AFFICHE_FILENAME,GS_AFFICHE_DATE,GS_AFFICHE_RECORDID,GS_AFFICHE_TIME) VALUES(?,?,?,?,?,?,?)";

        Connection conn = null;
        boolean tResult = false;
        try {
        	conn = this.getConnetion();
        	PreparedStatement pstmt = conn.prepareStatement(tSql);
        	pstmt.setString(1,aGSAfficheBean.getGs_affiche_id());
        	pstmt.setString(2,aGSAfficheBean.getGs_affiche_title());
        	String content = aGSAfficheBean.getGs_affiche_content();
        	pstmt.setCharacterStream(3,
                    new InputStreamReader(new ByteArrayInputStream(content.
                            getBytes("UTF-8")),"UTF-8"), content.length());
        	pstmt.setString(4,aGSAfficheBean.getGs_affiche_filename());
        	pstmt.setString(5,aGSAfficheBean.getGs_affiche_date());
        	pstmt.setString(6,aGSAfficheBean.getGs_affiche_recordid());
        	pstmt.setString(7,aGSAfficheBean.getGs_affiche_time());
        	
            pstmt.executeUpdate();
            pstmt.close();
            tResult = true;
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        } finally{
        	try{
        		conn.close();
        	}catch(Exception e){
        		
        	}
        }

        return tResult;        

    }
    
    /**
     * 在公告表GS_AFFICHE中更新一笔记录
     * 
     * @param aGSBalanceFeeBean
     * @throws CUPSException]
     * @return boolean
     */
    public boolean modify(GSAfficheBean aGSAfficheBean) throws GSException {

        String tSql = "UPDATE GS_AFFICHE SET GS_AFFICHE_TITLE = ?,GS_AFFICHE_CONTENT=?," +
                "GS_AFFICHE_FILENAME=?,GS_AFFICHE_DATE=? WHERE GS_AFFICHE_ID = ?";
     
        Connection conn = null;
        boolean tResult = false;
        try {
        	conn = this.getConnetion();
        	PreparedStatement pstmt = conn.prepareStatement(tSql);
        	pstmt.setString(1,aGSAfficheBean.getGs_affiche_title());
        	String content = aGSAfficheBean.getGs_affiche_content();
        	pstmt.setCharacterStream(2,
                    new InputStreamReader(new ByteArrayInputStream(content.
                            getBytes("UTF-8")),"UTF-8"), content.length());
        	pstmt.setString(3,aGSAfficheBean.getGs_affiche_filename());
        	pstmt.setString(4,aGSAfficheBean.getGs_affiche_date());
        	pstmt.setString(5,aGSAfficheBean.getGs_affiche_id());
            pstmt.executeUpdate();
            pstmt.close();
            tResult = true;
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        } finally{
        	try{
        		conn.close();
        	}catch(Exception e){
        		
        	}
        }

        return tResult;
    }
    
    /**
     * 在公告表GS_AFFICHE中删除一笔记录
     * 
     * @param aTypeNo
     *            序列号
     */
    public boolean delete(String aAfficheId) throws GSException {
        String tSql = "delete from GS_AFFICHE where GS_AFFICHE_ID=?";
        Object[] tObject = { aAfficheId };
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 查询一个公告表信息
     * @param aFeeTypeNo
     * @return
     * @throws GSException
     */
    public GSAfficheBean getAffiche(String aAfficheId) throws GSException {
        String tSql = "select * from GS_AFFICHE where GS_AFFICHE_ID =?";
        ArrayList tList = null;
        GSAfficheBean tGSAfficheBean = null;
        Object[] tObj = {aAfficheId};
        try{
            tList = this.executeQuery(new GSAfficheBean(),tSql,tObj);
            tGSAfficheBean= (GSAfficheBean)tList.get(0);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
                
        return tGSAfficheBean;
    }
    
    /**
     * 按照某个条件查询所有公告表
     * @return
     * @throws GSException
     */
    public ArrayList queryOtherAffiche(GSAfficheForm aGSAfficheForm) throws GSException{
        ArrayList tList = new ArrayList();
       
        StringBuffer tSql = new StringBuffer("select * from GS_AFFICHE where 1=1");
        
        if(aGSAfficheForm.getGs_affiche_title() != null && !"".equalsIgnoreCase(aGSAfficheForm.getGs_affiche_title()))
            tSql.append(" and GS_AFFICHE_TITLE like '%" + aGSAfficheForm.getGs_affiche_title() + "%'");
        
        if(aGSAfficheForm.getGs_affiche_content() != null && !"".equalsIgnoreCase(aGSAfficheForm.getGs_affiche_content()))
            tSql.append(" and GS_AFFICHE_CONTENT like '%" + aGSAfficheForm.getGs_affiche_content() + "%'");
        
        if(!(aGSAfficheForm.getGs_start_date().equals("00000000")&&aGSAfficheForm.getGs_end_date().equals("99999999"))){
            tSql.append(" and to_char(to_date(GS_AFFICHE_DATE,'YYYY-MM-DD HH24:mi:ss'),'YYYYMMDD') >='" + aGSAfficheForm.getGs_start_date() + "'");
            tSql.append(" and to_char(to_date(GS_AFFICHE_DATE,'YYYY-MM-DD HH24:mi:ss'),'YYYYMMDD') <='" + aGSAfficheForm.getGs_end_date() + "'");
        }
        tSql.append(" order by GS_AFFICHE_TIME desc");
        try{
            tList = this.executeQuery(new GSAfficheBean(),tSql.toString(),null);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
}
